package com.parallax;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewTreeObserver;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.ListView;

import com.nineoldandroids.animation.ValueAnimator;

public class ParallaxListView extends ListView {
    private int maxHeight;  // TODO: 2017/12/28 是由图片高度和ImageView高度共同决定的
    private ImageView imageView;
    private int originalHeight;//todo ImageView最初的高度

    public ParallaxListView(Context context) {
        super(context);
    }

    public ParallaxListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ParallaxListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setParallaxImageView(final ImageView imageView) {
        this.imageView = imageView;

        //todo 设定最大高度,比用onMeasure()测量好
        imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);// FIXME: 移除监听
                originalHeight = imageView.getHeight();// FIXME: 2017/12/28 获取imageView高度 用getMeasureHeight或树监听,注意要移除监听
                Log.e("tag", "originalHeight: " + originalHeight);
                int drawableHeight = imageView.getDrawable().getIntrinsicHeight();//todo 获取图片的高度
                maxHeight = originalHeight > drawableHeight ?
                        originalHeight * 2 : drawableHeight;
            }
        });

    }

    /**
     * todo 滚动view都有这个方法 在listview滑动到头的时候执行，可以获取到继续滑动的距离和方向
     * deltaX：继续滑动x方向的距离
     * todo deltaY：到头后继续滑动y方向的距离     负：表示顶部到头   正：表示底部到头
     * maxOverScrollX:x方向最大可以滚动的距离
     * maxOverScrollY：y方向最大可以滚动的距离
     * todo isTouchEvent: true: 是手指拖动滑动     false:表示fling靠惯性滑动;
     * <p>
     * todo listView滑动有两种方式,一种是拖动滑动,一种是靠惯性滑动
     */
    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
                                   int scrollY, int scrollRangeX, int scrollRangeY,
                                   int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        Log.e("执行", "overScrollBy--> deltaY:" + deltaY + "--isTouchEvent:" + isTouchEvent);
        if (deltaY < 0 && isTouchEvent) {
            //表示顶部到头，并且是手动拖动到头的情况
            //我们需要不断的增加ImageView的高度
            if (imageView != null) {
                int newHeight = imageView.getHeight() - deltaY / 3;
                if (newHeight > maxHeight)  // FIXME: 2017/12/28 限制最大高度
                    newHeight = maxHeight;
                imageView.getLayoutParams().height = newHeight;
                imageView.requestLayout();//todo 使ImageView的布局参数生效
            }
        }

        //		return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
        //				scrollRangeY, maxOverScrollX, 100, isTouchEvent);
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
                scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // TODO: 2017/12/28 松开弹回到最初高度
        if(ev.getAction()==MotionEvent.ACTION_UP){
        			//todo 需要将ImageView的高度缓慢恢复到最初高度
        			ValueAnimator animator = ValueAnimator.ofInt(imageView.getHeight(),originalHeight);
            // TODO: 2017/12/28 动画更新监听,imageView动画运动的原因
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        				@Override
        				public void onAnimationUpdate(ValueAnimator animator) {
        					//todo 获取动画的值，设置给imageview
        					int animatedValue = (Integer) animator.getAnimatedValue();

        					imageView.getLayoutParams().height = animatedValue;
        					imageView.requestLayout();//使ImageView的布局参数生效
        				}
        			});
        			animator.setInterpolator(new OvershootInterpolator(5));//todo 弹性的插值器
        			animator.setDuration(350);
        			animator.start();
        		}
        return super.onTouchEvent(ev);
    }
}
